{ "cells": [ { "cell_type": "markdown", "id": "b542bcd7", "metadata": {}, "source": [ "## Counter" ] }, { "cell_type": "code", "execution_count": 5, "id": "04f25a1d", "metadata": {}, "outputs": [], "source": [ "from collections import Counter" ] }, { "cell_type": "markdown", "id": "8ca6aa1a", "metadata": {}, "source": [ "### Logic" ] }, { "cell_type": "markdown", "id": "81ebbf83", "metadata": {}, "source": [ "- Counter(pass any iterable here)\n", " - It will give dictionary of keys(elements) and values(count)\n", " - Accessing the count:\n", " - Since it returns dict,you can use counter.keys(),counter.values() and counter.items()\n", " - or if you want count of specific key,you can do counter()['that key']" ] }, { "cell_type": "markdown", "id": "2393dba7", "metadata": {}, "source": [ "### Example" ] }, { "cell_type": "code", "execution_count": 30, "id": "220861d8", "metadata": {}, "outputs": [], "source": [ "l=[1,2,3,3,4,5,5,5,5,5,5,5,5,5,5]" ] }, { "cell_type": "code", "execution_count": 31, "id": "cd9c9ed7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dict_keys([1, 2, 3, 4, 5])\n", "dict_values([1, 1, 2, 1, 10])\n", "dict_items([(1, 1), (2, 1), (3, 2), (4, 1), (5, 10)])\n" ] } ], "source": [ "print(Counter(l).keys())\n", "print(Counter(l).values())\n", "print(Counter(l).items())" ] }, { "cell_type": "markdown", "id": "d5dd3db8", "metadata": {}, "source": [ "### Counting in lists" ] }, { "cell_type": "code", "execution_count": 15, "id": "8508bd97", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Counter({1: 1, 2: 1, 3: 2, 4: 1, 5: 10})" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l=[1,2,3,3,4,5,5,5,5,5,5,5,5,5,5]\n", "Counter(l)" ] }, { "cell_type": "markdown", "id": "64167bc7", "metadata": {}, "source": [ "### Counting in String" ] }, { "cell_type": "code", "execution_count": 8, "id": "e3d0a941", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Counter({'s': 2, 'a': 2, 'h': 2, 'i': 2, 'l': 2})" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s='sahilsahil'\n", "Counter(s)" ] }, { "cell_type": "markdown", "id": "61161e13", "metadata": {}, "source": [ "### Counting in dictionary" ] }, { "cell_type": "code", "execution_count": 4, "id": "4ba0820e", "metadata": {}, "outputs": [], "source": [ "d={\n", " 'a':10,\n", " 'b':20,\n", " 'c':40\n", "}" ] }, { "cell_type": "code", "execution_count": 28, "id": "159d3512", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Counter({'a': 10, 'b': 20, 'c': 40})" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Counter(d)" ] }, { "cell_type": "markdown", "id": "c2edcb1d", "metadata": {}, "source": [ "### Getting count of specific element" ] }, { "cell_type": "code", "execution_count": 29, "id": "2f1095b0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "40" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Counter(d)['c']" ] }, { "cell_type": "markdown", "id": "c1ed8d48", "metadata": {}, "source": [ "### Extra Functions we can use on Counter object" ] }, { "cell_type": "code", "execution_count": 32, "id": "2935940e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[(6, 5)]\n", "[(6, 5), (2, 2)]\n" ] } ], "source": [ "l=[1,2,2,3,4,4,5,6,6,6,6,6]\n", "print(Counter(l).most_common(1)) # highest occurence(count)\n", "print(Counter(l).most_common(2)) # top 2 based on occurence(count)" ] }, { "cell_type": "markdown", "id": "7765ba86", "metadata": {}, "source": [ "### More" ] }, { "cell_type": "markdown", "id": "a4754400", "metadata": {}, "source": [ "- Since it gives dict object,we can use all the dict operations and perform conditions and everything" ] }, { "cell_type": "code", "execution_count": 38, "id": "6336976c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[6]" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[k for k,v in Counter(l).items() if v==5]" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.8" } }, "nbformat": 4, "nbformat_minor": 5 }